package Euler43;

import java.util.*;

import ReusableCode.PrimeFunctions;

import com.google.common.collect.*;

public class PandigitalPerms {
	public static void main(String[] args) {
		List<Integer> primes = PrimeFunctions.GetPrimes(17);
		Iterator<List<String>> iter = Collections2.permutations(Arrays.asList("0 1 2 3 4 5 6 7 8 9".split(" "))).iterator();
		List<Long> nums = new ArrayList<Long>();
		long sum = 0;
		
		while(iter.hasNext())
		{
			List<String> val = iter.next();
			
			for(int i = 1; i < 8; i++)
			{
				int prime = primes.get(i-1);
				int num = Integer.parseInt(val.get(i) + val.get(i+1) + val.get(i+2));
				
				if(num%prime==0)
				{
					if(i==7)
					{
						String temp = "";
						for(int j = 0; j < val.size(); j++)
						{
							temp += val.get(j);
						}
						long number = Long.parseLong(temp);
						sum += number;
						nums.add(number);
					}
				}
				else
				{
					break;
				}
			}
		}
		
		System.out.println(nums);
		System.out.println("Sum is: " + sum);
	}
}
